<?php

class PetSkill
{
	private static $IDs=array(110101=>1,110102=>1,110103=>1,110104=>1,110201=>1,110202=>1,110203=>1,110204=>1,110205=>1,110301=>1,110302=>1,110303=>1,110310=>1,120101=>1,120102=>1,120103=>1,120104=>1,120105=>1,120106=>1,120107=>1,120201=>1,120202=>1,120203=>1,120204=>1,130101=>1,130102=>1,130103=>1,130104=>1,130105=>1,130106=>1,130201=>1,130202=>1,130203=>1,130301=>1,130302=>1,140101=>1,140102=>1,140103=>1,140104=>1,140201=>1,140202=>1,140203=>1,140204=>1,140205=>1,140301=>1,140302=>1,150101=>1,150102=>1,150103=>1,150104=>1,150105=>1,150106=>1,150201=>1,150202=>1,150301=>1,150302=>1,160101=>1,160102=>1,160103=>1,160104=>1,160105=>1,160106=>1,160107=>1,160201=>1,160202=>1,170101=>1,170102=>1,170201=>1,170202=>1,170301=>1,170302=>1,170303=>1,170901=>1,170902=>1,);

	public static function loadPetSkill($id)
	{
		if(!self::exists($id))
			throw new CException("unknown petSkill[$id]");

		$classname='PetSkill_'.$id;
		return call_user_func(array($classname, 'getInstance'));
	}

	public static function exists($id)
	{
		return array_key_exists($id, self::$IDs);
	}
    
    public function apply($self, &$enemy)
    {
        if($this->level==0)
            return false;
            
        $consumeMp = $this->consumeMp();
        if ($thos->mp < $consumeMp)
            return false;
        
        if(!$this->applySucc())
            return new FightPetActionResult($this->name);

        $effects = $this->effects($self,$enemy,$this->level);
        if($enemy->defending){
            foreach($effects as &$effect) {
                if($this->causeEnemyDamage($effect))
                    $effect->value=int($effect->value/4);
            }
        }        
        $effects[] = new FightPetActionEffect('self','mp','change',-$consumeMp);
        return new FightPetActionResult($this->name, $effects);
    }

    private function applySucc()
    {
        if($this->succRatio>=100)
            return true;
        $ratio=mt_rand(1, 100);
        return $ratio<=$this->succRatio ? true:false;
    }

    private function causeEnemyDamage(&$effect)
    {
        return $effect->target=='enemy'&& 
                $effect->attribute=='hp'&& 
                $effect->changeOrSet=='change'&& 
                $effect->value<0;
    }
    
    protected function effects(&$self,&$enemy,$skillLevel)
    {
        assert(false);
    }
    
    protected function consumeMp()
    {
        assert(false);
    }
}

?>
